#!/bin/bash

ARG=$1

function logmsg {
    /usr/bin/logger -p user.info udevmnt $ARG: $@
}

if [ -z $1 ] ; then
    logmsg "no device supplied. Exiting."
    exit 1 ;
fi

DEVICESHORT=$1
DEVICE=/dev/$1

# Wait for device to settle or something, or device is sometimes not mounted
sleep 0.25

if [ ! -e $DEVICE ] ; then
    logmsg "device file $DEVICE doesn\'t exist! Exiting."
    exit
fi


TMP=`/sbin/blkid -o udev $DEVICE`
for t in $TMP; do
    VAR=`echo $t | gawk -F = '{print $1}'`
    case $VAR in
        'ID_FS_LABEL')
            LABEL=`echo $t | gawk -F = '{print $2}'`
            ;;
        'ID_FS_TYPE')
            FSTYPE=`echo $t | gawk -F = '{print $2}'`
            ;;
        'ID_FS_UUID')
            UUID=`echo $t | gawk -F = '{print $2}'`
            ;;
    esac
done

logmsg "fstype is $FSTYPE, label is $LABEL, UUID is $UUID."

FSTAB=`cat /etc/fstab | cut -d' ' -f1`
for f in $FSTAB; do
    if [ $DEVICE == $f ] || [ UUID=$UUID == $f ]; then
        logmsg "$DEVICE is in fstab. Exiting."
        exit 1
    fi
done

if [ -z $FSTYPE ]; then
    logmsg "device file $DEVICE isn\'t a filesystem. Exiting."
    exit
fi

if [ -e /media/.udevmnt/$DEVICESHORT ] ; then
    # Test if device is mounted
    for i in `cat /proc/mounts | cut -d' ' -f1`; do
        if [ "x$DEVICESHORT" = "x$i" ]; then
            logmsg "$DEVICE is already mounted. Exiting."
            exit
        fi
    done
    /bin/rm /media/.udevmnt/$DEVICESHORT
    logmsg "stale file /media/.devmnt/$DEVICESHORT deleted."
fi

# Use volume label as mountpoint, or use device name if missing or already existing

if [ -z $LABEL ]; then
    MOUNTPOINT=/media/$DEVICESHORT
else
    MOUNTPOINT=/media/$LABEL
fi

if [ -e $MOUNTPOINT ] ; then
    # Test if mountpoint has something mounted there
    logmsg "$MOUNTPOINT already exists, testing for mounted filesystems."
    for i in `cat /proc/mounts | cut -d' ' -f2`; do
        if [ "x$MOUNTPOINT" = "x$i" ]; then
            logmsg "something already mounted at $MOUNTPOINT, using /media/$DEVICESHORT as mountpoint"
            MOUNTPOINT=/media/$DEVICESHORT
            if [ -e $MOUNTPOINT ] ; then
                logmsg "something already mounted at $MOUNTPOINT too! Exiting."
                exit
            fi
        fi
    done
fi

logmsg "creating mountpoint $MOUNTPOINT for device $DEVICE"
/bin/mkdir -p $MOUNTPOINT

case $FSTYPE in
    'vfat')
        /bin/mount -t vfat \
        -o rw,flush,dirsync,noexec,nodev,noatime,quiet,shortname=lower,gid=storage,dmask=0002,fmask=0113 \
        $DEVICE $MOUNTPOINT
        RETURNVAL=$?
        ;;
    'ntfs')
        /bin/mount -t ntfs -o ro,noexec,gid=storage,fmask=0113,dmask=0002 $DEVICE $MOUNTPOINT
        RETURNVAL=$?
        ;;
    *)
        /bin/mount -t auto -o rw,async,dirsync,noexec,nodev,noatime $DEVICE $MOUNTPOINT
        RETURNVAL=$?
        ;;
esac

if [ $RETURNVAL = 0 ]; then
    /bin/chown root:storage $MOUNTPOINT
    /bin/chmod 775 $MOUNTPOINT

    logmsg "$DEVICE mounted at $MOUNTPOINT"
    logmsg "creating /media/.udevmnt/$DEVICESHORT"
    /bin/mkdir -p /media/.udevmnt
    echo $MOUNTPOINT > /media/.udevmnt/$DEVICESHORT
else
    /bin/rmdir $MOUNTPOINT
    logmsg "mount failed, deleting $MOUNTPOINT"
fi
